package models.data;

import com.avaje.ebean.*;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import sun.security.krb5.internal.util.KrbDataInputStream;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kristborga
 * Date: 29.4.2013
 * Time: 13:49
 * To change this template use File | Settings | File Templates.
 */
@Entity
public class Vidhengitaflanemendur extends Model {
    @Id
    public int vidhengi_id;

    @Constraints.Required
    public int verkefnaskil_id;

    @Constraints.Required
    public String nafn;

    @Constraints.Required
    public byte[] vidhengi;

    @Constraints.Required
    public String nemandi_id;

    @Constraints.Required
    public int verkefnihops_id;

    public String contenttype;

    public Vidhengitaflanemendur(int verkefnaskil_id, String nafn, byte[] vidhengi, String nemandi_id, int verkefnihops_id, String contenttype) {
        this.vidhengi_id = createId();
        this.verkefnaskil_id = verkefnaskil_id;
        this.nafn = nafn;
        this.vidhengi = vidhengi;
        this.nemandi_id = nemandi_id;
        this.verkefnihops_id = verkefnihops_id;
        this.contenttype = contenttype;
    }
    public static Vidhengitaflanemendur create(int verkefnaskil_id, String nafn, File vidhengi, String nemandi_id, int verkefnihops_id, String contenttype){
        byte[] fileContent = new byte[(int) vidhengi.length()];
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            FileInputStream fis = new FileInputStream(vidhengi.getPath());
            for (int readNum; (readNum = fis.read(fileContent)) != -1;) {
                bos.write(fileContent, 0, readNum); //no doubt here is 0
            }
        }
        catch (IOException ex) {;
        }

        Vidhengitaflanemendur vidhengitaflanemendur = new Vidhengitaflanemendur(verkefnaskil_id, nafn, bos.toByteArray(),nemandi_id,verkefnihops_id,contenttype);
        vidhengitaflanemendur.save();
        return vidhengitaflanemendur;
    }
    public static Finder<Integer, Vidhengitaflanemendur> find = new Finder<Integer, Vidhengitaflanemendur>(
            Integer.class, Vidhengitaflanemendur.class
    );

    public static List<byte[]> getVidhengi(int verkefnaskil_id){
        List<Vidhengitaflanemendur> vidhengi = Vidhengitaflanemendur.find.where().eq("verkefnaskil_id", verkefnaskil_id).findList();
        List<byte[]> toReturn = new ArrayList<byte[]>();
        for(Vidhengitaflanemendur vid : vidhengi){
            toReturn.add(vid.getVidhengiVariable());
        }
        return toReturn;
    }
    public static List<Vidhengitaflanemendur> getVidhengiAll(int verkefnaskil_id){
        return Vidhengitaflanemendur.find.where().eq("verkefnaskil_id",verkefnaskil_id).findList();
    }
    public static Vidhengitaflanemendur getVidhengiAllByName(int verkefnaskil_id, String nafn){
        return Vidhengitaflanemendur.find.where().eq("verkefnaskil_id",verkefnaskil_id).where().eq("nafn", nafn).findUnique();
    }
    public static List<Vidhengitaflanemendur> getVidhengiList(int verkefnaskil_id){
        List<Vidhengitaflanemendur> vidhengi = Vidhengitaflanemendur.find.where().eq("verkefnaskil_id", verkefnaskil_id).findList();
        return vidhengi;
    }
    public static List<Vidhengitaflanemendur> getVidhengiListByVerkefniHops(int verkefnihops_id){
        return Vidhengitaflanemendur.find.where().eq("verkefnihops_id",verkefnihops_id).findList();
    }
    public static void deleteVidhengi(int vidhengi_id){

        Vidhengitaflanemendur vidhengi = Vidhengitaflanemendur.find.where().eq("vidhengi_id", vidhengi_id).findUnique();
        vidhengi.delete();

    }

    public static boolean vidhengiVerkefniHopsEmpty(int verkefnihops_id){
        int count = Vidhengitaflanemendur.find.where().eq("verkefnihops_id",verkefnihops_id).findRowCount();
        if(count == 0){
            return false;
        }
        else{
            return true;
        }
    }
    public static Integer createId(){
        List<Object> vidhenginemenda = Vidhengitaflanemendur.find.findIds();

        Integer max = 1;
        for(Object vidhengi : vidhenginemenda){
            if(Integer.valueOf(vidhengi.toString()) > max) {
                max = Integer.valueOf(vidhengi.toString());
            }
        }
        return (max+1);
    }

    public int getVidhengi_id() {
        return vidhengi_id;
    }

    public void setVidhengi_id(int vidhengi_id) {
        this.vidhengi_id = vidhengi_id;
    }

    public int getVerkefnaskil_id() {
        return verkefnaskil_id;
    }

    public void setVerkefnaskil_id(int verkefnaskil_id) {
        this.verkefnaskil_id = verkefnaskil_id;
    }

    public String getNafn() {
        return nafn;
    }

    public void setNafn(String nafn) {
        this.nafn = nafn;
    }

    public byte[] getVidhengiVariable() {
        return vidhengi;
    }

    public void setVidhengiVariable(byte[] vidhengi) {
        this.vidhengi = vidhengi;
    }

    public String getNemandi_id() {
        return nemandi_id;
    }

    public void setNemandi_id(String nemandi_id) {
        this.nemandi_id = nemandi_id;
    }

    public int getVerkefnihops_id() {
        return verkefnihops_id;
    }

    public void setVerkefnihops_id(int verkefnihops_id) {
        this.verkefnihops_id = verkefnihops_id;
    }

    public String getContenttype() {
        return contenttype;
    }

    public void setContenttype(String contenttype) {
        this.contenttype = contenttype;
    }
}

